package algorithms4g

import "fmt"

func init() {
	s := longestPalindrome("abbadce")
	fmt.Println("最长回文字符串子串", s)
}

func longestPalindrome(s string) string {
	len := len(s)
	if len < 2 {
		return s
	}
	maxLen := 1
	start := 0
	for i, _ := range s {
		for j, _ := range s {
			if j-i+1 > maxLen && isPalindrome(s, i, j) {
				maxLen = j - i + 1
				start = i
			}
		}
	}
	rs := []rune(s)
	return string(rs[start : start+maxLen])

}

func isPalindrome(s string, i int, j int) bool {
	for i < j {
		if s[i] != s[j] {
			return false
		}
		i++
		j--
	}
	return true
}
